import QtQuick
import QtQuick.Controls
//import Qt.labs.platform

import "./gui"
import "./as_qml"

Window {
    id: root
    width: 900
    height: 580
    visible: true
    title: qsTr("opencv学习工具")

    property int toolWinNum: 0
    property int helpWinNum: 0

    function checkToolWinOnly(qml) {
        if(toolWinNum > 0) {
            myHandle.warningWindow("打开工具页面出错", "已存在一个已打开的工具页面\n请先关闭它再打开新的页面")
        } else {
            toolWinNum++
            createToolWinLoader.source = qml
        }
    }

    function checkHelpWinOnly(qml) {
        if(helpWinNum > 0) {
            myHandle.warningWindow("打开帮助页面出错", "已存在一个已打开的帮助页面\n请先关闭它再打开新的页面")
        } else {
            helpWinNum++
            createHelpWinLoader.source = qml
        }
    }

    Shortcut {
        sequence: "Alt+T"

        onActivated: toolMenu.open()
    }

    Shortcut {
        sequence: "Alt+H"

        onActivated: helpMenu.open()
    }

    Rectangle {
        id: topMenuBar
        width: parent.width
        height: 28
        color: "#d2d4d6"

        AsMenuBar {

            AsMenu {
                id: toolMenu
                title: qsTr("工具(T)")

                AsMenuItem {
                    text: qsTr("预处理图片")

                    onTriggered: {
                        checkToolWinOnly("gui/ImageRetreatment.qml")
                    }
                }

                AsMenuItem {
                    text: qsTr("生成csv文件")

                    onTriggered: {
                        checkToolWinOnly("gui/CreateCsv.qml")
                    }
                }

                AsMenuItem {
                    text: qsTr("生成识别器模型")

                    onTriggered: {
                        checkToolWinOnly("gui/CreateRecognizer.qml")
                    }
                }
            }

            AsMenu {
                id: helpMenu
                title: qsTr("帮助(H)")

                AsMenuItem {
                    text: qsTr("使用说明")

                    onTriggered: {
                        checkHelpWinOnly("gui/helpWin.qml")
                    }
                }

                AsMenuItem {
                    text: qsTr("关于软件")

                    onTriggered: {
                        checkHelpWinOnly("gui/aboutWin.qml")
                    }
                }
            }
        }
    }

    Loader {
        id: createToolWinLoader

        onLoaded: {
            createToolWinSignal.target = createToolWinLoader.item
        }

        Connections {
            id: createToolWinSignal

            function onClosing() {
                toolWinNum--
                createToolWinLoader.source = ""
            }
        }
    }

    Loader {
        id: createHelpWinLoader

        onLoaded: {
            createHelpWinSignal.target = createHelpWinLoader.item
        }

        Connections {
            id: createHelpWinSignal

            function onClosing() {
                helpWinNum--
                createHelpWinLoader.source = ""
            }
        }
    }

    ShowVideoMain {
        width: parent.width
        anchors.top: topMenuBar.bottom
        anchors.bottom: parent.bottom
        visible: true
    }

}
